package com.app.couponsharelife.ui.main.home.local

import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.app.couponsharelife.R
import com.app.couponsharelife.base.BaseActivity
import com.app.couponsharelife.ui.viewmodel.LocalViewModel
import com.app.fuquan.data.bean.PageStatus
import kotlinx.android.synthetic.main.activity_local_shop_pay_result.*
import kotlinx.android.synthetic.main.common_tool_bar_layout.*
import java.util.HashMap

class LocalShopPayResultActivity : BaseActivity() {

    override val viewLayoutId: Int get() = R.layout.activity_local_shop_pay_result

    private var mViewModel: LocalViewModel? = null

    private var shopName = ""

    private var mobile = ""

    private var money = ""

    private var status = false

    override fun initViews() {
        if (intent.getStringExtra("mobile") != null)
            mobile = intent.getStringExtra("mobile")
        else {
            showToastMsg("数据错误，请退出重试")
            finish()
        }
        if (intent.getStringExtra("shopName") != null)
            shopName = intent.getStringExtra("shopName")
        else {
            showToastMsg("数据错误，请退出重试")
            finish()
        }
        if (intent.getStringExtra("money") != null)
            money = intent.getStringExtra("money")
        else {
            showToastMsg("数据错误，请退出重试")
            finish()
        }
        status = intent.getBooleanExtra("status", false)
        tvLocalShopName.text = shopName
        tvPayMoney.text = money
        initToolBar()
    }

    private fun initToolBar() {
        ivBack.setOnClickListener { finish() }
        tvToolBarTitle.text = "支付结果"
    }

    override fun initViewModel() {
        if (mViewModel == null) mViewModel = ViewModelProviders.of(this).get(LocalViewModel::class.java)
        if (!mViewModel!!.getLifeNewCard()!!.hasObservers()) {
            mViewModel!!.getLifeNewCard()!!.observe(this, Observer {
                if (it == null) {
                    dismissProgressDialog()
                    showToastMsg("数据出错啦")
                    return@Observer
                }
                when (it.status) {
                    PageStatus.Loading -> {
                        showProgressDialog("请稍候")
                    }
                    PageStatus.Error -> {
                        dismissProgressDialog()
                        mViewModel!!.freshGetLifeNewCard(hashMapOf(), false)
                        showToastMsg(parseErrorDate(it.error!!.message).tip)
                    }
                    PageStatus.Empty -> {
                        dismissProgressDialog()
                        mViewModel!!.freshGetLifeNewCard(hashMapOf(), false)
                        showToastMsg("暂无数据")
                    }
                    PageStatus.Content -> {
                        dismissProgressDialog()
                        mViewModel!!.freshGetLifeNewCard(hashMapOf(), false)
                        if (it.data!!.code == 200 && it.data.data != null) {
                            println("获取成功")
                            tvCouponMoney.text = it.data.data!!.content
                            tvCouponType.text = if (it.data.data!!.cardType == "CASH") "代金券" else "折扣券"
                            tvCouponDetail.text = it.data.data!!.condition
                        } else {
                            println("获取失败")
                        }
                    }
                }
            })
        }
        getLifeNewCard(mobile)
    }

    private fun getLifeNewCard(phone: String) {
        val map = HashMap<String, String>()
        map["mobile"] = phone
        mViewModel?.freshGetLifeNewCard(map, true)
    }
}
